package com.chase.chasecat.service.impl;

import com.chase.chasecat.common.R;
import com.chase.chasecat.dto.LoginDTO;
import com.chase.chasecat.entity.UserEntity;
import com.chase.chasecat.service.LoginService;
import com.chase.chasecat.utils.JwtHelper;
import com.chase.chasecat.utils.UserUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private RedisTemplate<String, UserEntity> redisTemplate;

    @Override
    public R login(LoginDTO loginDTO) {
        UserEntity user = new UserEntity();
        // 存入本地变量中
        UserUtil.setUser(user);
        // 根据用户id创建token
        String token = JwtHelper.createToken(user.getId());
        // 存入redis中
        redisTemplate.opsForValue().set(token, user);
        return R.ok("登录成功");
    }
}
